/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "MLabel.h"

namespace gui
{

MLabel::~MLabel(void)
{
}


/* Render */
void MLabel::Render( IRender* r )
{
    Vector3f bg = GetBackgroundColor();
    Vector3f fg = GetForegroundColor();


    // set background color
    r->SetColor( bg.GetX(), bg.GetY(), bg.GetZ(), GetAlpha() );

    Vector2f pos = GetLocation();
    r->FillRect( pos.x, pos.y, GetWidth(), GetHeight() );

    int charWidth = r->GetTextWidth( 'H' );
    size_t length = charWidth * m_labelText.length();

    std::string str;
    if ( length > GetWidth() )
    {
        size_t remaining = (GetWidth() - length) / charWidth;
        size_t npos = m_labelText.length() - remaining;
        str = m_labelText.substr( 0, npos );
    } 
    else
    {
        str = m_labelText;
    }
    r->SetColor( fg.GetX(), fg.GetY(), fg.GetZ() );
    r->DrawString( pos.x, pos.y, str.c_str() );

    // now draw all sub components
    gui::Component::Render( r );
}

};